/**
 * 视图操作
 * */
layui.define(['globalConfig', 'routes'], function (exports) {
    let router = layui.routes;
    let globalConfig = layui.globalConfig;

    let body = ".layui-layout-admin>.layui-body";
    let tab = body + ">.layui-tab";
    let scroll = ".layui-layout-admin>.layui-side>.layui-side-scroll";
    let header = ".layui-layout-admin>.layui-header";

    let sysViews = {
        flexible: function(view) {
            let clz = $(".layui-layout-admin").hasClass("admin-nav-mini");
            if (clz == !view) {
                return
            }
            if (view) {
                sysViews.hideTableScrollBar();
                $(".layui-layout-admin").removeClass("admin-nav-mini")
            } else {
                $(".layui-layout-admin").addClass("admin-nav-mini")
            }
            sysViews.removeNavHover();
            sysViews.resizeTable(360)
        },
        rollPage: function(s) {
            let title = $(tab + ">.layui-tab-title");
            let scrollLeft = title.scrollLeft();
            if ("left" === s) {
                title.animate({
                    "scrollLeft": scrollLeft - 120
                }, 100)
            } else {
                if ("auto" === s) {
                    let width = 0;
                    title.children("li").each(function() {
                        if ($(this).hasClass("layui-this")) {
                            return false
                        } else {
                            width += $(this).outerWidth()
                        }
                    });
                    title.animate({
                        "scrollLeft": width - 120
                    }, 100)
                } else {
                    title.animate({
                        "scrollLeft": scrollLeft + 120
                    }, 100)
                }
            }
        },
        refresh: function(path) {
            router.refresh(path)
        },
        getPageHeight: function() {
            return document.documentElement.clientHeight || document.body.clientHeight
        },
        getPageWidth: function() {
            return document.documentElement.clientWidth || document.body.clientWidth
        },
        hideTableScrollBar: function(q) {
            if (sysViews.getPageWidth() > 750) {
                if (window.hsbTimer) {
                    clearTimeout(hsbTimer)
                }
                let p = globalConfig.pageTabs ? $(tab + ">.layui-tab-content>.layui-tab-item.layui-show") : $(body);
                p.find(".layui-table-body.layui-table-main").addClass("no-scrollbar");
                window.hsbTimer = setTimeout(function() {
                    p.find(".layui-table-body.layui-table-main").removeClass("no-scrollbar")
                }, q == undefined ? 500 : q)
            }
        },
        resizeTable: function(p) {
            setTimeout(function() {
                let q = globalConfig.pageTabs ? $(tab + ">.layui-tab-content>.layui-tab-item.layui-show") : $(body);
                q.find(".layui-table-view").each(function() {
                    let r = $(this).attr("lay-id");
                    layui.table && layui.table.resize(r)
                })
            }, p == undefined ? 0 : p)
        },
        activeNav: function(path) {
            if (!path) {
                path = location.hash
            }
            if (path && path != "") {
                $(scroll + ">.layui-nav .layui-nav-item .layui-nav-child dd").removeClass("layui-this");
                $(scroll + ">.layui-nav .layui-nav-item").removeClass("layui-this");
                let scrollNav = $(scroll + '>.layui-nav a[href="#' + path + '"]');
                if (scrollNav && scrollNav.length > 0) {
                    if ($(scroll + ">.layui-nav").attr("lay-accordion") == "true") {
                        $(scroll + ">.layui-nav .layui-nav-itemed").removeClass("layui-nav-itemed")
                    }
                    scrollNav.parent().addClass("layui-this");
                    scrollNav.parent("dd").parents(".layui-nav-child").parent().addClass("layui-nav-itemed");

                    $('ul[lay-filter="admin-side-nav"]').addClass("layui-hide");
                    let nav = scrollNav.parents(".layui-nav");
                    nav.removeClass("layui-hide");
                    $(header + ">.layui-nav>.layui-nav-item").removeClass("layui-this");
                    $(header + '>.layui-nav>.layui-nav-item>a[nav-bind="' + nav.attr("nav-id") + '"]').parent().addClass("layui-this");
                    let height = nav.offset().top + scrollNav.outerHeight() + 30 - sysViews.getPageHeight();
                    let top = 50 + 65 - scrollNav.offset().top;
                    if (height > 0) {
                        $(scroll).animate({
                            "scrollTop": $(scroll).scrollTop() + height
                        }, 100)
                    } else {
                        if (top > 0) {
                            $(scroll).animate({
                                "scrollTop": $(scroll).scrollTop() - top
                            }, 100)
                        }
                    }
                } else {}
            } else {
                console.warn("active url is null")
            }
        },
        removeNavHover: function() {
            $(".admin-nav-hover>.layui-nav-child").css({
                "top": "auto",
                "max-height": "none",
                "overflow": "auto"
            });
            $(".admin-nav-hover").removeClass("admin-nav-hover")
        },
        setNavHoverCss: function(hover) {
            let child = $(".admin-nav-hover>.layui-nav-child");
            if (hover && child.length > 0 && hover.offset()) {
                let t = (hover.offset().top + child.outerHeight()) > window.innerHeight;
                if (t) {
                    let q = hover.offset().top - child.outerHeight() + hover.outerHeight();
                    if (q < 50) {
                        let s = sysViews.getPageHeight();
                        if (hover.offset().top < s / 2) {
                            child.css({
                                "top": "50px",
                                "max-height": s - 50 + "px",
                                "overflow": "auto"
                            })
                        } else {
                            child.css({
                                "top": r.offset().top,
                                "max-height": s - hover.offset().top,
                                "overflow": "auto"
                            })
                        }
                    } else {
                        child.css("top", q)
                    }
                } else {
                    child.css("top", hover.offset().top)
                }
                j = true
            }
        },
        btnLoading: function(q, r, s) {
            if (r != undefined && (typeof r == "boolean")) {
                s = r;
                r = undefined
            }(s == undefined) && (s = true);
            let p = $(q);
            if (s) {
                r && p.html(r);
                p.find(".layui-icon").addClass("layui-hide");
                p.addClass("icon-btn");
                p.prepend('<i class="layui-icon layui-icon-loading layui-anim layui-anim-rotate layui-anim-loop ew-btn-loading"></i>');
                p.prop("disabled", "disabled")
            } else {
                p.find(".ew-btn-loading").remove();
                p.removeProp("disabled", "disabled");
                if (p.find(".layui-icon.layui-hide").length <= 0) {
                    p.removeClass("icon-btn")
                }
                p.find(".layui-icon").removeClass("layui-hide");
                r && p.html(r)
            }
        },
        openSideAutoExpand: function() {
            $(".layui-layout-admin>.layui-side").off("mouseenter.openSideAutoExpand").on("mouseenter.openSideAutoExpand", function() {
                if ($(this).parent().hasClass("admin-nav-mini")) {
                    sysViews.flexible(true);
                    $(this).addClass("side-mini-hover")
                }
            });
            $(".layui-layout-admin>.layui-side").off("mouseleave.openSideAutoExpand").on("mouseleave.openSideAutoExpand", function() {
                if ($(this).hasClass("side-mini-hover")) {
                    sysViews.flexible(false);
                    $(this).removeClass("side-mini-hover")
                }
            })
        },
        openCellAutoExpand: function() {
            $("body").off("mouseenter.openCellAutoExpand").on("mouseenter.openCellAutoExpand", ".layui-table-view td", function() {
                $(this).find(".layui-table-grid-down").trigger("click")
            });
            $("body").off("mouseleave.openCellAutoExpand").on("mouseleave.openCellAutoExpand", ".layui-table-tips>.layui-layer-content", function() {
                $(".layui-table-tips-c").trigger("click")
            })
        },
		resize(view){
			let id = '#' + view;
			let footer = $('.layui-footer').css('display');
			if(footer == 'block'){
				$(id).css('height', 'calc(100vh - 172px)')
			}else{
				$(id).css('height', 'calc(100vh - 129px)')
			}
		},
    }

    exports('sysViews', sysViews);
})